﻿using System;
using System.Net;
using System.Collections.Generic;

namespace SBPweb.PhotoGallery.Common
{

    public class WebClientPool
    {

        #region Private fields

        private List<WebClient> clients = new List<WebClient>();

        #endregion

        #region Constructor logic

        public WebClientPool()
        {
        }

        public WebClientPool(int size)
        {
            lock (clients)
            {
                for (int i = 0; i < size; i++)
                {
                    clients[i] = InitializeWebClient();
                }
            }
        }

        #endregion

        #region Methods

        public WebClient GetClientInstance()
        {
            lock (clients)
            {
                if (clients.Count == 0 || clients[0].IsBusy)
                {
                    clients.Insert(0, InitializeWebClient());
                }
                return clients[0];
            }
        }

        public void SetClientBusy(WebClient client)
        {
            lock (clients)
            {
                if (!client.IsBusy)
                {
                    clients.Insert(clients.Count - 1, client);
                }
            }
        }

        private WebClient InitializeWebClient()
        {
            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
            client.OpenWriteCompleted += new OpenWriteCompletedEventHandler(client_OpenWriteCompleted);
            client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);
            client.WriteStreamClosed += new WriteStreamClosedEventHandler(client_WriteStreamClosed);
            return client;
        }

        private void ReactivateWebClient(WebClient client)
        {
            if (client.IsBusy)
            {
                return;
            }
            lock (clients)
            {
                clients.Remove(client);
                clients.Insert(0, client);
            }
        }

        private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            ReactivateWebClient(sender as WebClient);
        }

        private void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            ReactivateWebClient(sender as WebClient);
        }

        private void client_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e)
        {
            ReactivateWebClient(sender as WebClient);
        }

        private void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            ReactivateWebClient(sender as WebClient);
        }

        private void client_WriteStreamClosed(object sender, WriteStreamClosedEventArgs e)
        {
            ReactivateWebClient(sender as WebClient);
        }

        #endregion

    }

}